package com.lantosec.hmrp.modules.revisit.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.lantosec.hmrp.core.kit.PageVO;
import com.lantosec.hmrp.core.kit.Result;
import com.lantosec.hmrp.modules.revisit.entity.RevisitConfig;
import com.lantosec.hmrp.modules.revisit.mapper.RevisitConfigMapper;
import com.lantosec.hmrp.modules.revisit.service.IRevisitConfigService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lantosec.hmrp.modules.revisit.vo.RevisitConfigVO;
import com.lantosec.hmrp.modules.system.entity.SysDept;
import com.lantosec.hmrp.modules.system.service.ISysDeptService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.context.annotation.Primary;
import lombok.extern.log4j.Log4j2;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

/**
 * <p>
 * 回访功能配置 服务实现类
 * </p>
 *
 * @author AutoCode
 * @since 2020-04-30
 */
@Service
@Primary
@Log4j2
public class RevisitConfigServiceImpl extends ServiceImpl<RevisitConfigMapper, RevisitConfig> implements IRevisitConfigService {
    @Autowired
    private ISysDeptService iSysDeptService;

    @Override
    public PageVO<RevisitConfigVO> getAll(PageVO<RevisitConfig> page,String deptId,Integer revisitDoctor,Integer revisitNurse) {
        page = this.page(page,new QueryWrapper<RevisitConfig>().eq(deptId != null,RevisitConfig.ColName.dept_id.toString(),deptId)
                                                                .eq(revisitDoctor != null,RevisitConfig.ColName.revisit_doctor.toString(),revisitDoctor)
                                                                .eq(revisitNurse != null,RevisitConfig.ColName.revisit_nurse.toString(),revisitNurse));
        PageVO<RevisitConfigVO> revisitConfigVOPage = new PageVO<>();
        List<RevisitConfig> revisitConfigList = page.getRecords();
        List<RevisitConfigVO> revisitConfigVOList = new ArrayList<>();
        revisitConfigList.forEach(parm->{
            RevisitConfigVO revisitConfigVO = parm.convertToVO();
            String deptIds = parm.getDeptId();
            SysDept sysDept = iSysDeptService.getById(deptIds);
            revisitConfigVO.setDeptName(sysDept.getDeptName());
            revisitConfigVOList.add(revisitConfigVO);
        });
        revisitConfigVOPage.setRecords(revisitConfigVOList);
        revisitConfigVOPage.setTotal(page.getTotal());
        revisitConfigVOPage.setPageNo(page.getPageNo());
        revisitConfigVOPage.setPageSize(page.getPageSize());
        return revisitConfigVOPage;
    }

    @Override
    public Result getDeptId(String deptId) {
        RevisitConfig revisitConfig = this.getOne(new QueryWrapper<RevisitConfig>().eq(RevisitConfig.ColName.dept_id.toString(),deptId));
        if(Objects.isNull(revisitConfig)){
            return Result.ok("部门不存在",true);
        }else {
            return Result.ok("部门已存在",false);
        }
    }
}
